﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Common.Utility;
using ZhongKeSite.Entity;
using ZhongKeSite.Entity.Models;
using ZhongKeSite.Repository;

namespace ZhongKeSite.Service
{
    public class NewsService : INewsService
    {
        INewsRepository newsRepository;
        public NewsService(INewsRepository _newsRepository)
        {
            newsRepository = _newsRepository;
        }

        #region 后台-News
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LayPager<News> GetNewsPagedList(int pageIndex, int pageSize, Expression<Func<News, bool>> predicate)
        {
            var list = newsRepository.GetPagedList(pageIndex, pageSize, predicate);
            LayPager<News> pmodel = new LayPager<News>();
            if (list != null)
            {
                pmodel.count = list.TotalItemCount;
                pmodel.msg = CommonConst.Success;
                pmodel.data = list.ToList();
            }
            return pmodel;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultResponse<News> GetNews(string id)
        {
            ResultResponse<News> resultResponse = new ResultResponse<News>() { success = false, msg = CommonConst.Fail };
            resultResponse.data = newsRepository.GetById(id);
            if (resultResponse.data != null)
            {
                resultResponse.success = true;
                resultResponse.msg = CommonConst.Success;
            }
            return resultResponse;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse AddNews(News model, IPathProvider pathProvider)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            model.ID = IdentityCreator.NextIdentity;
            model.AddDate = DateTime.Now;
            string newsUrl = string.Format("{0}/detail?id={1}", AppSettings.Domain, model.ID);
            model.QrCodePath = ImageHelper.GetQrCodeImgUrl(pathProvider, newsUrl);
            response.success = newsRepository.Add(model);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity"></param>
        public ResultResponse UpdateNews(News model)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            if (string.IsNullOrWhiteSpace(model.Title))
            {
                response.msg = "标题不能为空!";
                return response;
            }
            var o = newsRepository.GetById(model.ID);
            o.Title = model.Title;
            o.Content = model.Content;
            o.CoverImage = model.CoverImage;
            o.IsAvailable = model.IsAvailable;
            o.IsRecommend = model.IsRecommend;
            o.NewsType = model.NewsType;
            o.Author = model.Author;
            o.PublishDate = model.PublishDate;
            o.Remark = model.Remark;
            o.SEOTitle = model.SEOTitle;
            o.SEOKeyword = model.SEOKeyword;
            o.SEODescription = model.SEODescription;
            o.SortNo = model.SortNo;
            o.UpdateBy = model.UpdateBy;
            o.UpdateDate = DateTime.Now;

            response.success = newsRepository.Update(o);
            response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            return response;
        }

        /// <summary>
        /// 推荐，取消推荐 操作
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="dataStatus"></param>
        /// <returns></returns>
        public ResultResponse UpdateNewsState(List<string> ids, string userName, DataStatus dataStatus)
        {
            ResultResponse response = new ResultResponse() { success = false, msg = CommonConst.Fail };
            var products = newsRepository.GetList(s => ids.Contains(s.ID)).ToList();
            if (products != null && products.Count > 0)
            {
                switch (dataStatus)
                {
                    case DataStatus.Recommend:
                    case DataStatus.UnRecommend:
                        products.ForEach(k =>
                        {
                            k.IsRecommend = DataStatus.Recommend == dataStatus ? 1 : 0;
                            k.UpdateBy = userName;
                            k.UpdateDate = DateTime.Now;
                        });
                        response.success = newsRepository.Update(products);
                        break;
                    case DataStatus.Deleted:
                        products.ForEach(k =>
                        {
                            k.IsDel = 1;
                            k.UpdateBy = userName;
                            k.UpdateDate = DateTime.Now;
                        });
                        response.success = newsRepository.Update(products);
                        break;
                }
                response.msg = response.success ? CommonConst.Success : CommonConst.Fail;
            }
            return response;
        }
        #endregion

        #region 前台-News
        public NewsInfo GetNewsInfo()
        {
            NewsInfo newsInfo = new NewsInfo();
            News news = newsRepository.GetList(k => k.IsRecommend == 1 && k.IsAvailable == 1 && k.IsDel == 0).OrderBy(s => s.SortNo).FirstOrDefault();
            Expression<Func<News, bool>> predicate = s => s.IsAvailable == 1 && s.IsDel == 0;
            if (news != null)
            {
                predicate = predicate.And(k => k.ID != news.ID);
            }
            newsInfo.RecommendNews = news;
            newsInfo.NewsList = newsRepository.GetPagedList(1, 12, predicate).ToList();
            return newsInfo;
        }
        #endregion

    }
}
